import datetime,cgi
import sys
import os
from google.appengine.ext.webapp import template
from google.appengine.ext import db
from google.appengine.ext import webapp
from google.appengine.api import users
from google.appengine.ext.webapp.util import run_wsgi_app
from google.appengine.dist import use_library
from os import environ
from models import *

class NextReviewMain(webapp.RequestHandler):
	def post(self):
		pteams=''
		date=self.request.get("date")
		allteams=db.GqlQuery("SELECT * FROM teamDetails")
		for team in allteams:
			if(self.request.get(team.teamid)=='teams'):
				if(pteams!=''):
					pteams=pteams+","+team.teammember
				else:
					pteams=team.teammember
		notemembers=pteams.split(",")
		message="Your team has a review on '%s'" %date
		for memberid in notemembers:
			newnote=notificationDetails(admissionnumber=memberid,notification=message)
			key=newnote.put()
			idnum=key.id()
			newnote.notificationid=str(idnum)
			newnote.put()
		self.redirect("/nextreview")

class NextReview(webapp.RequestHandler):
	def post(self):
		selcategory=self.request.get("category")
		catid=str(selcategory)
		projects=db.GqlQuery("SELECT * FROM projectDetails WHERE category='%s'" %catid)
		details=[]
		#self.response.out.write(projects)	
		for project in projects:
			teamid=project.teamid
			pname=project.projectname
			members=db.GqlQuery("SELECT * FROM teamDetails WHERE teamid='%s'" %teamid)
			mem=members
			for i in mem:
				listmembers=i.teammember
				members=listmembers.split(",")
				membernames=''
				for member in members:
					indmember=db.GqlQuery("SELECT * FROM userDetails WHERE admissionnumber='%s'" %member)
					for mem in indmember:
						membernames=membernames+mem.username

			tmp=[]
			tmp.append(teamid)
			tmp.append(pname)
			tmp.append(membernames)
			details.append(tmp)
		path = os.path.join(os.path.dirname(__file__),'nextreviewmain.html')
		self.response.out.write(template.render(path,{"members":details}))

	def verifyLogin(self):
		user = users.get_current_user()
		if user:
			usercheck=userDetails.all()
			usercheck.filter("email =",user.email())
			try:
				currentuser=usercheck.fetch(1)[0]
				userdet=db.GqlQuery("SELECT * FROM userDetails WHERE email='"+user.email()+"'")
				for i in userdet:
					if i.privilege=='admin':
						return
					elif i.approved==False:
						self.redirect("/usererror")
						return
				self.redirect("/user")
			except:
				path = os.path.join(os.path.dirname(__file__),'form.html')#use template
				self.response.out.write(template.render(path,{"email":user.email()}))#using template
		else:
			self.redirect(users.create_login_url(self.request.uri))

	def get(self):
		self.verifyLogin()
		category=[]		
		allcategories=db.GqlQuery("SELECT * FROM projectCategory")
		for spcategory in allcategories:
			tmp=[]
			tmp.append(spcategory.categoryid)
			tmp.append(spcategory.categorytype)
			category.append(tmp)
		path = os.path.join(os.path.dirname(__file__),'nextreview.html')#use template
		self.response.out.write(template.render(path,{"category":category}))#using template

application = webapp.WSGIApplication([('/nextreview', NextReview),('/nextreviewmain', NextReviewMain)], debug=True)

def main():
	run_wsgi_app(application)

if __name__ == "__main__":
	main()
